﻿// See https://aka.ms/new-console-template for more information
//Console.WriteLine("Hello, World!");
using System;
using System.IO;
using System.Reflection;
using System.Runtime.Loader;

class Program
{
    static void Main(string[] args)
    {
        var folder = Path.Combine(Environment.CurrentDirectory, "Animals");
        var files = Directory.GetFiles(folder);
        var animalTypes = new List<Type>();
        foreach (var file in files)
        {
            var assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(file);
            var types = assembly.GetTypes();
            foreach (var t in types)
            {
                if (t.GetMethod("Voice") != null)
                {
                    animalTypes.Add(t);
                }
            }
        }

        while (true)
        {
            for (var i = 0; i < animalTypes.Count; i++)
            {
                Console.WriteLine($"{i + 1}.{animalTypes[i].Name}");
            }
            Console.WriteLine("==================");
            Console.WriteLine("Please choose animal:");
            var index = int.Parse(Console.ReadLine());
            if (index > animalTypes.Count || index < 1)
            {
                Console.WriteLine("No such an animal. Try again!");
                continue;
            }

            Console.WriteLine("How many times?");
            var times = int.Parse(Console.ReadLine());
            var t = animalTypes[index - 1];
            var m = t.GetMethod("Voice");
            var o = Activator.CreateInstance(t);
            m.Invoke(o, new object[] { times });
        }
    }
}
